package com.example.first.bottombarone;

import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;

import com.ashokvarma.bottomnavigation.BadgeItem;
import com.ashokvarma.bottomnavigation.BottomNavigationBar;
import com.ashokvarma.bottomnavigation.BottomNavigationItem;

public class MainActivity extends AppCompatActivity implements BottomNavigationBar.OnTabSelectedListener {


    BottomNavigationBar mBottomNavigationBar;
    private IndexFragment indexFragment;
    private MapFragment mapFragment;
    private LoveFragment loveFragment;
    private PersonFragment personFragment;
    private BadgeItem badgeItem;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initBadge();
        setDefaultFragment();
        initNavigationBar();
    }

    private void initNavigationBar() {
        mBottomNavigationBar = (BottomNavigationBar) findViewById(R.id.bottom_navigation_bar);
        mBottomNavigationBar.setTabSelectedListener(this);
        mBottomNavigationBar.setMode(BottomNavigationBar.MODE_FIXED);
        mBottomNavigationBar.setBackgroundStyle(BottomNavigationBar.BACKGROUND_STYLE_RIPPLE);
        mBottomNavigationBar
                .addItem(new BottomNavigationItem(R.drawable.icon_index, "首页").setActiveColorResource(R.color.red))
                .addItem(new BottomNavigationItem(R.drawable.icon_map, "地图").setActiveColorResource(R.color.blue))
                .addItem(new BottomNavigationItem(R.drawable.icon_love, "关注").setActiveColorResource(R.color.yellow).setBadgeItem(badgeItem))
                .addItem(new BottomNavigationItem(R.drawable.icon_person, "个人").setActiveColorResource(R.color.green))
                .setFirstSelectedPosition(0)
                .initialise();
    }

    public void initBadge() {
        badgeItem = new BadgeItem()
                .setBorderWidth(2)//边框宽度
                .setBorderColor("#ff0000")//边框颜色
                .setBackgroundColor("#ff0000")//背景颜色
                .setGravity(Gravity.RIGHT | Gravity.TOP)//位置
                .setText("2")//显示文字
                .setTextColor("#ffffff")//文字颜色
                .setAnimationDuration(2000)//渐退、渐出的时间
                .setHideOnSelect(true);//选中时是否消失
    }

    private void setDefaultFragment() {
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        indexFragment = new IndexFragment();
        transaction.replace(R.id.fragment_container, indexFragment);
        transaction.commit();
    }

    @Override
    public void onTabSelected(int position) {
        Log.d("onTabSelected", "onTabSelected: " + position);
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        switch (position) {
            case 0:
                if (indexFragment == null) {
                    indexFragment = new IndexFragment();
                }
                transaction.replace(R.id.fragment_container, indexFragment);
                break;
            case 1:
                if (mapFragment == null) {
                    mapFragment = new MapFragment();
                }
                transaction.replace(R.id.fragment_container, mapFragment);
                break;
            case 2:
                if (loveFragment == null) {
                    loveFragment = new LoveFragment();
                }
                transaction.replace(R.id.fragment_container, loveFragment);
                break;
            case 3:
                if (personFragment == null) {
                    personFragment = new PersonFragment();
                }
                transaction.replace(R.id.fragment_container, personFragment);
                break;
            default:
                break;
        }
        // 事务提交
        transaction.commit();
    }

    @Override
    public void onTabUnselected(int position) {
        Log.d("onTabUnselected", "onTabUnselected: " + position);
    }

    @Override
    public void onTabReselected(int position) {
        Log.d("onTabReselected", "onTabReselected: " + position);
    }
}